package com.kimo.shop.admin.controller;


import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kimo.shop.bean.dto.FinanceDetailsDto;
import com.kimo.shop.bean.dto.RevenueOverviewDto;
import com.kimo.shop.bean.dto.StationDto;
import com.kimo.shop.bean.param.FinanceDetailsParam;
import com.kimo.shop.bean.param.RevenueOverviewParam;
import com.kimo.shop.common.i18n.I18nMessage;
import com.kimo.shop.common.util.PageParam;
import com.kimo.shop.common.util.PoiExcelUtil;
import com.kimo.shop.service.RevenueOverviewService;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * 财务管理—营收概况
 *
 * @author kimo-taozhouchuan
 * @date 2020-08-17
 */

@RestController
@RequestMapping("/platform/financialManagement")
public class RevenueOverviewController {

    @Autowired
    private RevenueOverviewService revenueOverviewService;

    @GetMapping("/getStation")
    public ResponseEntity<List<StationDto>> getStation(Integer shopId){
        List<StationDto> fetchDto = revenueOverviewService.getStationList(shopId);
        return ResponseEntity.ok(fetchDto);
    }

    @GetMapping("/getIncomeProfile")
    public ResponseEntity<RevenueOverviewDto> getIncomeProfile(RevenueOverviewParam param) throws ParseException {
        RevenueOverviewDto result = revenueOverviewService.getData(param);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/getFinanDetail")
    public ResponseEntity<IPage<FinanceDetailsDto>> getFinancialDetails(PageParam<FinanceDetailsDto> page, FinanceDetailsParam param) {
        IPage<FinanceDetailsDto> result = revenueOverviewService.getPageDetail(page, param);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/getFinanDetailForm")
    public void getFinanDetailForm(FinanceDetailsParam param, HttpServletResponse response) {
        List<FinanceDetailsDto> result = revenueOverviewService.getFinanDetailForm(param);
        excel(result, response);
    }


    public void excel(List<FinanceDetailsDto> param, HttpServletResponse response) {
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getBigWriter();

        // 报表列名称数组
        String[] hearder = {I18nMessage.getMessage("kimo.finance.transactionTime"), I18nMessage.getMessage("kimo.finance.orderNumber"), I18nMessage.getMessage("kimo.finance.incOrPay"), I18nMessage.getMessage("kimo.finance.payType"), I18nMessage.getMessage("kimo.finance.transactionAmount")};

        Sheet sheet = writer.getSheet();
        //时间列的宽度
        sheet.setColumnWidth(0, 20 * 256);
        //订单编号列的宽度
        sheet.setColumnWidth(1, 20 * 256);
        sheet.setColumnWidth(2, 20 * 256);
        sheet.setColumnWidth(3, 20 * 256);
        sheet.setColumnWidth(4, 20 * 256);
//        //剩余列的宽度
//        for (int i = 2; i <= param.size(); i++) {
//            sheet.setColumnWidth(i, 10 * 256);
//        }
        //设置报表名称
        writer.merge(hearder.length - 1, I18nMessage.getMessage("kimo.finance.reportTitle"));
        writer.writeRow(Arrays.asList(hearder));
        int row = 1;
        //循环插入每一行的数据
        for (FinanceDetailsDto dto : param) {
            int firstRow = ++row;
            int lastRow = firstRow;
            int col = -1;

            mergeIfNeed(writer, firstRow, lastRow, ++col, col, dto.getTransDate());
            mergeIfNeed(writer, firstRow, lastRow, ++col, col, dto.getOrderId());
            mergeIfNeed(writer, firstRow, lastRow, ++col, col, dto.getIncPayType().equals("1") ? I18nMessage.getMessage("kimo.finance.income") : I18nMessage.getMessage("kimo.finance.pay"));
            mergeIfNeed(writer, firstRow, lastRow, ++col, col, I18nMessage.getMessage("kimo.payType.pay"+dto.getPayType()));
            mergeIfNeed(writer, firstRow, lastRow, ++col, col, dto.getTransAmount());
        }
        PoiExcelUtil.writeExcel(response, writer);
    }

    /**
     * 如果需要合并的话，就合并
     */
    public void mergeIfNeed(ExcelWriter writer, int firstRow, int lastRow, int firstColumn, int lastColumn, Object content) {
        if (lastRow - firstRow > 0 || lastColumn - firstColumn > 0) {
            writer.merge(firstRow, lastRow, firstColumn, lastColumn, content, false);
        } else {
            writer.writeCellValue(firstColumn, firstRow, content);
        }

    }
}
